package lib.wzh.com.libcore.framework.threadpool;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author lib
 * @Date 2021/12/18 22:28
 * @ClassName MallExecutor
 * @Version v1.0
 */

@EnableConfigurationProperties(MallExecutorConfigurationProperties.class)
@Configuration(proxyBeanMethods = false)
public class MallExecutor {

    /** 自定义线程池
     * @author lib
     * @date 2021/12/18 22:32
     * @param config 自定义线程池的配置
     * @since: jdk1.8
     * @update [序号][日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    @Bean
    @ConditionalOnProperty(prefix = "mall.thread", name = {"core-pool-size","max-pool-size"})
    public ThreadPoolExecutor threadPoolExecutor(MallExecutorConfigurationProperties config){
        //corePoolSize 核心线程数--固定的
        //maximumPoolSize 最大线程数
        //keepAliveTime 空闲线程存活时间 释放的线程= 最大线程数-核心线程数
        //TimeUnit 时间单位
        //BlockingQueue 线程不足时的，请求放置队列的大小
        //threadFactory 执行程序创建新线程时使用的工厂
        //RejectedExecutionHandler 由于达到线程边界和队列容量而阻塞执行时要使用的处理程序
       return new ThreadPoolExecutor(config.getCorePoolSize(),
               config.getMaxPoolSize(),
               config.getKeepAliveTime(),
               config.getUnit(),
               new LinkedBlockingDeque<>(),
               Executors.defaultThreadFactory(),
               new ThreadPoolExecutor.AbortPolicy());
    }

}
